#! /bin/sh

#  Copyright (c) International Business Machines  Corp., 2002
#
#  This program is free software;  you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY;  without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
#  the GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program;  if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
#  02/05/03  Modified - Manoj Iyer <manjo@mail.utexas.edu> use USCTEST macros
#			fixed bugs.
#  07/26/05  Michael Reed  <mreedltp@vnet.ibm.com>
#			Made changes to account for the replacement of syslogd
#			with syslog-ng
#
##################################################################
# case1: Test whether messages are logged to the specified file  #
# in the configuration file.					 #
#								 #
# Send messages to syslogd at some level and facility	 	 #
# and grep for those messages.					 #
#								 #
# syslog.conf should contain:					 #
# *.crit		/usr/adm/critical			 #
# mail.info		/usr/spool/adm/syslog			 #
##################################################################

. syslog-lib.sh || exit 1

syslog_case1()
{
	tst_resm TINFO "testing whether messages are logged into log file"

	# Create the configuration file specific to this test case.

	case "$CONFIG_FILE" in
	/etc/syslog.conf|/etc/rsyslog.conf)
		echo "$RSYSLOG_CONFIG" > $CONFIG_FILE
		echo "*.crit		/var/log/messages" >> $CONFIG_FILE
		echo "mail.info	$MAILLOG" >> $CONFIG_FILE
		;;

	/etc/syslog-ng/syslog-ng.conf)
		echo "source src{ internal(); unix-dgram(\"/dev/log\"); \
		      udp(ip(\"0.0.0.0\") port(514)); };" > $CONFIG_FILE
		echo " " >> $CONFIG_FILE
		echo " " >> $CONFIG_FILE
		echo "# Added for syslog testcase"  >> $CONFIG_FILE
		echo "filter f_syslog	 { level(crit);};" >> $CONFIG_FILE
		echo "filter f_syslogMail { level(info)	and facility(mail); };" >> $CONFIG_FILE
		echo "destination syslog-messages { file(\"/var/log/messages\");};" >> $CONFIG_FILE
		echo "log { source(src); filter(f_syslog); destination(syslog-messages); };" >> $CONFIG_FILE
		echo "destination syslog-mail { file(\"$MAILLOG\");};" >> $CONFIG_FILE
		echo "log { source(src); filter(f_syslogMail); destination(syslog-mail); };"  >> $CONFIG_FILE
		;;
	esac

	restart_syslog_daemon

	# Grepping pattern has to be changed whenever the executable name
	# changes, ex: syslogtst executable.
	# This check is neccessary for syslog-ng because $MAILLOG is
	# only created after syslogtst
	if [ -e "$MAILLOG" ]; then
		oldvalue1=`grep -c "syslogtst: mail info test" $MAILLOG`
	else
		oldvalue1=0
	fi

	# Call syslogtst executable with case number as argument
	if syslogtst 1 2>/dev/null; then

		sleep 2

		if [ ! -e $MAILLOG ]; then
			tst_resm TBROK "$MAILLOG no such log file"
			cleanup 1
		fi

		newvalue1=`grep -c "syslogtst: mail info test" $MAILLOG`
		if [ "x$(( $newvalue1 - $oldvalue1 ))" != "x1" ]; then
			status_flag=1
		fi
	else
		status_flag=1
	fi

}

export TST_TOTAL=1
export TST_COUNT=1
export TCID=syslog01

tst_resm TINFO "Send messages to syslogd at some level "
tst_resm TINFO "and facility and grep for those messages."

setup
syslog_case1
cleanup ${status_flag:=0}
